/*
 * @Author: chunlai
 * @Date: 2025-02-06 15:33:48
 * @LastEditTime: 2025-02-11 15:29:58
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \0.newInsure\src\mixins\revenue.js
 * @Description:
 */
import { mapGetters } from "vuex";
export const revenueMinxin = {
  data() {
    return {
      showPicker: false,
      taxIdentDetail: [
        {
          snumber: "", //序号123
          crsTaxPlace: "", //税收居民国
          crsTaxPlaceName: "",
          noTaxNoReason: "", //是否存在纳税人识别号 C-存在；A-居民国（地区）不发放纳税人识别号；B-账户持有人未能取得纳税人识别号
          taxNo: "", //纳税人识别号 是否存在纳税人识别号值为C,必传
          noTaxNoReasonDetail: "", //未提供纳税识别号原因 是否存在纳税人识别号值为A,必传
          reason: "", //未取得纳税识别号具体原因 是否存在纳税人识别号值为B,必传
        },
      ],
      options: [],
      areaOptions: [],
      logoUrl: "",
      keyWord: "",
      sameToNow: false,
      target: "",
      targetIndex: "",
      nationList: [],
      cityList: [],
      errorMesg: "",
      reasonList: [
        {
          text: "居民国（地区）不发放纳税人识别号",
          keyId: "1",
        },
        {
          text: "账户持有人未能取得纳税人识别号",
          keyId: "2",
        },
      ],
      rightIcon: "",
      dialogPop: false,
      tipMsg: "",
      crsTaxPlaceObj: {}, // 税收居民国
    };
  },
  computed: {
    ...mapGetters({
      crsIdTypeList: "dictionary/crsIdTypeList",
      crsTaxPlace: "dictionary/crsTaxPlace",
      country: "dictionary/nationList",
    }),
    viewOptions() {
      if (this.options.length > 0) {
        return this.options.filter(
          (item) => item.text.indexOf(this.keyWord) !== -1
        );
      } else {
        return [];
      }
    },
    userLabel() {
      let res = "投保人";
      if (this.uType == "bbr") {
        res = "被保险人";
      }
      return res;
    },
  },
  watch: {
    taxIdentDetail: {
      handler(value) {
        value.forEach((item, index) => {
          item.snumber = `${index + 1}`;
          if (!item.crsTaxNo) {
            if (item.noTaxNoReason == "1") {
              item.noTaxNoReasonDetail = "";
            }
          } else {
            item.noTaxNoReason = "";
            item.noTaxNoReasonDetail = "";
          }
        });
        this.form.taxIdentDetail = value;
      },
      deep: true,
    },
  },
  methods: {
    validatorNo(val, rule) {
      // 识别号位数校验
      const findCounty = this.nationList.find(
        (ele) => ele.keyId === val.crsTaxPlace
      );
      if (findCounty && findCounty.tinHas && findCounty.tinHas === "1") {
        if (!findCounty.tinLength.includes(val.crsTaxNo.length)) {
          return false;
        }
      }
      // 其他校验
      if (val.crsTaxPlace == "GEO") {
        // 2居民个人纳税编号：11位字符
        // 1非居民个人纳税编号：9位字符
        if (
          (val.crsTaxNo.length !== 9 && this.form.crsIdType == "1") ||
          (val.crsTaxNo.length !== 11 && this.form.crsIdType == "2")
        ) {
          return false;
        }
      } else if (val.crsTaxPlace == "GGY") {
        // 根西岛 以GY作为前缀，后跟6位数字
        var reg = /^[G][Y]\d{6}$/;
        return reg.test(val.crsTaxNo);
      } else if (val.crsTaxPlace == "IMN") {
        // 马恩岛
        // 1.纳税人编号：HXXXXXX-XX（字母H+6位数字+1个连字符及2位后缀数字，2位后缀数字可省略）；
        var reg = /^[H]\d{6}[-][\d{2}]$/;
        return reg.test(val.crsTaxNo);
      } else if (val.crsTaxPlace == "LIE") {
        // 列支敦士登
        // 一般由 12 位数字构成，某些纳税人识别号可能是 7 位或 4 位数字，原因是省略了开头为零的数字
      } else if (val.crsTaxPlace == "POL") {
        // 波兰2.纳税人识别号：10位数字，最后一位是校验码
      } else if (val.crsTaxPlace == "SGP") {
        //   新加坡
        // 2.纳税参考号：ANNNNNNNC（N是数字，C是校验码）；
      } else if (val.crsTaxPlace == "TUR") {
        // 土耳其2.纳税人识别号：10位数字
      } else if (val.crsTaxPlace == "USA") {
        // 美国纳税人识别号：10位数字
      }
      return true;
    },
    // 纳税识别号校验
    blurEvent(itemObj, type) {
      if (type === "taxNo") {
        const findCounty = this.nationList.find(
          (ele) => ele.keyId === itemObj.crsTaxPlace
        );
        if (findCounty && findCounty.tinHas && findCounty.tinHas === "1") {
          if (!findCounty.tinLength.includes(itemObj.crsTaxNo.length)) {
            this.errorMesg = "纳税人识别号位数不正确！";
          } else {
            this.errorMesg = "";
          }
        }
      }
    },
    changeCrsIdType(val) {
      if (val == 1 || val == 2) {
        this.taxIdentDetail = [
          {
            snumber: "", //序号123
            crsTaxPlace: "", //税收居民国
            crsTaxPlaceName: "",
            noTaxNoReason: "", //是否存在纳税人识别号 C-存在；A-居民国（地区）不发放纳税人识别号；B-账户持有人未能取得纳税人识别号
            taxNo: "", //纳税人识别号 是否存在纳税人识别号值为C,必传
            noTaxNoReasonDetail: "", //未提供纳税识别号原因 是否存在纳税人识别号值为A,必传
            reason: "", //未取得纳税识别号具体原因 是否存在纳税人识别号值为B,必传
          },
        ];
      }
      this.form.crsIdTypeName = this.crsIdTypeList[val].text;
    },
    changeNoTaxReason(e, index) {
      console.log(e, index);
      this.$set(this.taxIdentDetail[index], "noTaxNoReason", e);
    },
    // 删除
    closeTax(index) {
      this.taxIdentDetail.splice(index, 1);
    },
    // 省市区弹窗
    showCityPicker(target) {
      this.keyWord = "";
      this.target = target;
      this.areaOptions = this.cityList;
      this.showPicker = true;
    },
    // 弹窗
    showPickAction(target, index) {
      this.keyWord = "";
      this.target = target;
      if (index >= 0) {
        this.targetIndex = index;
      }
      let list = this.nationList;
      // if (target == "noTaxNoReason") {
      //   list = this.reasonList;
      // }
      this.options = list;
      this.showPicker = true;
    },
    // 添加税号
    addList() {
      let data = {
        snumber: `${this.taxIdentDetail.length + 1}`, //序号123
        crsTaxPlace: "", //税收居民国
        noTaxNoReason: "", //是否存在纳税人识别号 C-存在；A-居民国（地区）不发放纳税人识别号；B-账户持有人未能取得纳税人识别号
        taxNo: "", //纳税人识别号 是否存在纳税人识别号值为C,必传
        notProReason: "", //未提供纳税识别号原因 是否存在纳税人识别号值为A,必传
        reason: "", //未取得纳税识别号具体原因 是否存在纳税人识别号值为B,必传
      };
      this.taxIdentDetail.push(data);
      this.$forceUpdate();
    },
    formatter(value) {
      // 过滤输入非字母和空格
      return value.replace(/^((?![ A-Za-z]).)*$/g, "");
    },
  },
};
